﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Linq;
using Common;
using EntityModel;

namespace Business
{
    public class ChannelBC : BusinessBase<Channel, UserModel>
    {        
        /// <summary>
        /// get all channels and area name of channel
        /// </summary>
        /// <returns>all channels and area name of them</returns>
        public List<Channel> GetChannelsMenu()
        {
            List<Channel> result = new List<Channel>();

            //get relevant repositories
            var repChannel = this.retreiveHelper.GetRepositoryWithoutTracking<Channel>();
            var repAreaRef = this.retreiveHelper.GetRepositoryWithoutTracking<AreaChannelRef>();
            var repArea = this.retreiveHelper.GetRepositoryWithoutTracking<AreaMaster>();

            //retrieve data
            var objQuery = (from channel in repChannel
                            join channelRef in repAreaRef on channel.ChannelID equals channelRef.ChannelID
                            join area in repArea on channelRef.AreaID equals area.AreaID
                            orderby area.SortOrder, channel.ShortName
                            select new
                            {
                                ChannelID = channel.ChannelID,
                                ChannelCode = channel.ChannelCode,
                                ShortName = channel.ShortName,
                                FullName = channel.FullName,
                                Description = channel.Description,
                                AreaID = area.AreaID,
                                AreaDescription = area.Description
                            }).ToList();

            //fill result into channel objects
            result = objQuery.Select(t => new Channel
            {
                ChannelID = t.ChannelID,
                ChannelCode = t.ChannelCode,
                ShortName = t.ShortName,
                FullName = t.FullName,
                Description = t.Description,
                AreaID = t.AreaID,
                AreaDescription = t.AreaDescription                
            }).ToList();

            //return the result
            return result;
        }

        public Result<Channel> GetAllByArea(Int32 areaID, string sortCol, SORT_ORDER sortOrder, Int16 currentPage, Int16 pageNumber)
        {
            var repChannel = this.retreiveHelper.GetRepositoryWithoutTracking<Channel>();
            var repAreaRef = this.retreiveHelper.GetRepositoryWithoutTracking<AreaChannelRef>();

            var source = (from channel in repChannel
                          join areaRef in repAreaRef on channel.ChannelID equals areaRef.ChannelID
                          where areaRef.AreaID == areaID
                          select channel).AsQueryable();

            return this.retreiveHelper.GetComplexPaging(source, sortCol, sortOrder, currentPage, pageNumber);
        }

    }
}
